package com.haoma.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoma.entity.LearningPath;
import com.haoma.excel.pojo.ExcelVo;
import com.haoma.excel.utils.ExcelUtil;
import com.haoma.mapper.LearningPathMapper;
import com.haoma.service.LearningPathService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 */
@Service
public class LearningPathServiceImpl extends ServiceImpl<LearningPathMapper, LearningPath> implements LearningPathService {

    @Autowired
    private LearningPathMapper learningPathMapper;

    /**
     * 分页查询学习路数据
     *
     * @param page
     * @param limit
     * @param name
     * @return
     */
    public IPage pageLearningPath(Integer page, Integer limit, String name) {
        // 设置分页参数
        Page<LearningPath> learningPathPage = new Page<>(page, limit);
        // 设置分页条件
        LambdaQueryWrapper<LearningPath> learningPathLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            learningPathLambdaQueryWrapper.like(LearningPath::getName, name);
        }
        learningPathLambdaQueryWrapper.orderByAsc(LearningPath::getStartTime);
        // 开始分页查询
        Page<LearningPath> learningPathPage1 = learningPathMapper.selectPage(learningPathPage, learningPathLambdaQueryWrapper);
        return learningPathPage1;
    }

    /**
     * 新增
     *
     * @param learningPath
     */
    public void saveLearningPath(LearningPath learningPath) {
        save(learningPath);

    }

    /**
     * 修改
     *
     * @param learningPath
     */
    public void updateLearningPath(LearningPath learningPath) {
        updateById(learningPath);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delLearningPath(Integer id) {
        this.removeById(id);
    }


    /**
     * 导出Excel
     *
     * @param response
     */
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 获取所有的数据
        List<LearningPath> learningPaths = list();
        // 生成数据
        List<ExcelVo> collect = learningPaths.stream().map(item -> {
            ExcelVo excelVo = new ExcelVo();
            BeanUtils.copyProperties(item, excelVo);
            return excelVo;
        }).collect(Collectors.toList());
        // 开始导出
        ExcelUtil.download(response, collect, ExcelVo.class);
    }

    /**
     * 导入Excel
     *
     * @param multipartFile
     */
    public void importExcel(MultipartFile multipartFile) throws IOException {
        List<ExcelVo> excelVoList = EasyExcel.read(multipartFile.getInputStream())
                // 映射数据
                .head(ExcelVo.class)
                // 读取工作表
                .sheet()
                // 读取并同步返回数据
                .doReadSync();
        System.out.println(excelVoList);
        // 将数据插入到数据库中
        List<LearningPath> list = excelVoList.stream().map(item -> {
            LearningPath learningPath = new LearningPath();
            BeanUtils.copyProperties(item, learningPath);
            return learningPath;
        }).toList();
        // 批量插入
        this.saveBatch(list);
    }

    /**
     * 下载Excel模板
     *
     * @param response
     */
    public void getExcelTemplate(HttpServletResponse response) {
        // 存储模板数据
        List<ExcelVo> excelVoList = new ArrayList<>();
        // 组成模板数据
        ExcelVo excelVo = new ExcelVo();
        // 存放
        excelVoList.add(excelVo);
        // 导出
        ExcelUtil.download(response, excelVoList, ExcelVo.class);
    }
}